package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dao.UserDao;
import com.woniu.entity.Course;
import com.woniu.entity.PageResult;
import com.woniu.entity.User;
import com.woniu.entity.UserAnswer;
import com.woniu.entity.vo.UserVo;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo<User> getUsersByPage(User user, int pageNum, int pageSize) {
        //不带条件查询
        // 开始分页(倒序)
//        PageHelper.startPage(pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize, "create_time desc");
        // 查询数据(带条件)
        List<User> users = userDao.selectByCondition(user);
//        List<User> users = userDao.selectAllUsers();
        // 封装分页结果
        return new PageInfo<>(users);
    }

    @Override
    public int addUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userDao.deleteUser(id);
    }

    @Override
    public User getUserById(Integer id) {
        return userDao.getUserById(id);
    }

    //修改用户状态
    @Override
    public int updateUserStatus(User user) {
        return userDao.updateUserStatus(user);
    }

    @Override
    public User selectByUserNameAndPassword(String username, String password) {
        User user = userDao.selectByUserName(username);
        if (user == null) {
            return null;
        } else {
            if (user.getPassword().equals(password)) {
                return user;
            } else {
                return null;
            }
        }

    }

    @Override
    public Boolean register(UserVo user) {
        User u = userDao.selectByUserName(user.getUsername());
        if (u == null) {
            //说明用户不存在，可以注册
            User newUser = new User();
            newUser.setUsername(user.getUsername());
            newUser.setPassword(user.getPassword());
            newUser.setEmail(user.getEmail());
            newUser.setRoleId(3);
            newUser.setStatus(1);
            userDao.insertUser(newUser);
            return true;
        }
        return false;
    }

    //查询教师列表
    @Override
    public List<User> getTeachers() {
        return userDao.selectTeachers();
    }

    @Override
    public PageResult getTeachers(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> allUser = userDao.getTeachers();
        PageInfo<User> pageInfo = new PageInfo<>(allUser);
        List<User> list = pageInfo.getList();
        PageResult pageResult = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(),pageInfo.getTotal(), pageInfo.getPages(), list);
        return pageResult;
    }
    //添加收藏
    @Override
    public void addCollect(Integer uid, Integer cid) {
        userDao.addCollect(uid,cid);
    }
    //取消收藏
    @Override
    public void delCollect(Integer uid, Integer courseId) {
        userDao.delCollect(uid,courseId);
    }

    @Override
    public void addUserAnswer(UserAnswer userAnswer) {
        userDao.addUserAnswer(userAnswer);
    }

    @Override
    public UserAnswer selectByUserIdAndVedioId(Integer userId, Integer videoId) {
       return userDao.selectByUserIdAndVedioId(userId,videoId);
    }

    @Override
    public void updateUserAnswer(UserAnswer userAnswer) {
        userDao.updateUserAnswer(userAnswer);
    }

    @Override
    public void deleteUserAnswer(Integer userId, Integer videoId) {
        userDao.deleteUserAnswer(userId,videoId);
    }

    @Override
    public PageResult selectByUserId(Integer userId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserAnswer> allUserAnswer = userDao.selectByUserId(userId);
        PageInfo<UserAnswer> pageInfo = new PageInfo<>(allUserAnswer);
        List<UserAnswer> list = pageInfo.getList();
        PageResult pageResult = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(),pageInfo.getTotal(), pageInfo.getPages(), list);
        return pageResult;
    }
}
